package com.lgCore.consumer.model;

import com.alibaba.fastjson.JSON;
import com.lgCore.model.ServiceInfaceData;
import com.lgCore.model.ServiceInfaceDetailData;
import com.lgCore.model.ZKBaseData;
import com.lgCore.model.ZKNodeType;
import com.lgCore.util.ZkUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * zk节点信息
 */
public class ClientZkData {
    // 服务调用次数
    private static int count = 1;

    //客户端zk信息
    public static ConcurrentHashMap<String, List<String>> clientZkDataHashMap = new ConcurrentHashMap<String, List<String>>();

    //增加
    public static void putData(String path, String data) {
        List<String> valueList = clientZkDataHashMap.get(path);
        if (valueList == null || valueList.isEmpty()) {
            valueList = new ArrayList<>();
        }
        for (String s : valueList) {
            if (data.equals(s)) {
                return;
            }
        }
        valueList.add(data);
        clientZkDataHashMap.put(path, valueList);
    }

    public static void editData(String path, String data) {
        List<String> valueList = clientZkDataHashMap.get(path);
        if (valueList != null && !valueList.isEmpty()) {
            for (int i = 0; i < valueList.size(); i++) {
                if (data.equals(valueList.get(i))) {
                    valueList.set(i, data);
                    return;
                }
            }
            clientZkDataHashMap.put(path, valueList);
        }
    }

    public static void removeData(String path, String data) {
        List<String> valueList = clientZkDataHashMap.get(path);
        if (valueList != null && !valueList.isEmpty()) {
            valueList = valueList.stream().filter(s -> !data.equals(s)).collect(Collectors.toList());
            clientZkDataHashMap.put(path, valueList);
        }
    }

    /**
     * 获取业务节点下的所有信息
     *
     * @param serverName
     * @return
     */
    public static List<String> getDataByPath(String serverName) {
        return clientZkDataHashMap.get(serverName);
    }

    /**
     * 轮训给节点值
     *
     * @param serverName
     * @return
     */
    public static String getValueByPath(String serverName) {
        List<String> list = getDataByPath(serverName);
        if (list == null || list.isEmpty()) {
            return null;
        }
        //轮训
        String data = list.get(count % list.size());
        count++;
        return data;
    }

    /**
     * 轮训给节点值
     *
     * @param list
     * @return
     */
    public static String getValueByTrain(List<String> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        //轮训
        String data = list.get(count % list.size());
        count++;
        return data;
    }

    /**
     * 业务抽离service名称
     * @return
     */
    public static String getServicePath(String servicePath){
        String[] serviceChild = servicePath.split("/");
        // TODO: 2020/11/6  这里需要修改，灰度发布 
//        if(serviceChild.length>3){
//            return serviceChild[serviceChild.length - 2];
//        }

        if(serviceChild.length==3){
            return serviceChild[serviceChild.length - 1];
        }
        return null;
    }


    public static void syncZkData(String type,String msg){
        if(StringUtils.isBlank(msg)){
            return;
        }
        ServiceInfaceData serviceInfaceData= JSON.parseObject(msg, ServiceInfaceData.class);
        ZKBaseData zkBaseData = JSON.parseObject(serviceInfaceData.getServicePath(), ZKBaseData.class);
        if (zkBaseData.getZkNodeType().getType().equals(ZKNodeType.INTERFACE.getType())) {

        } else if (zkBaseData.getZkNodeType().getType().equals(ZKNodeType.INTERFACE_DETAIL.getType())) {
            ServiceInfaceDetailData serviceInfaceDetailData = JSON.parseObject(serviceInfaceData.getServicePath(), ServiceInfaceDetailData.class);
            //zk地址唯一标识
            String zkIdea=  serviceInfaceData.getZkServicePath().substring(serviceInfaceData.getZkServicePath().lastIndexOf(":")+1);
            if("add".equals(type)) {
                ClientZkData.putData(serviceInfaceDetailData.getServicePath(),serviceInfaceDetailData.getIp()+":"+serviceInfaceDetailData.getNettyPort()+":"+zkIdea);
            }else if("edit".equals(type)) {
                ClientZkData.editData(serviceInfaceDetailData.getServicePath(),serviceInfaceDetailData.getIp()+":"+serviceInfaceDetailData.getNettyPort()+":"+zkIdea);
            }else if("del".equals(type)) {
                ClientZkData.removeData(serviceInfaceDetailData.getServicePath(),serviceInfaceDetailData.getIp()+":"+serviceInfaceDetailData.getNettyPort()+":"+zkIdea);
            }

        }






    }
}
